package hlhj.fhp.checkpoint.fgms

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AlertDialog
import android.util.Log
import android.view.View
import android.widget.RadioButton
import com.alipay.sdk.app.PayTask
import com.example.mymvp.log
import com.example.mymvp.mvp.BaseFgm
import com.example.mymvp.mvp.BaseModule
import com.example.mymvp.mvp.BasePresenter
import com.example.mymvp.mvp.BaseView
import com.google.gson.Gson
import hlhj.fhp.checkpoint.Constant
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.atys.AddCarAty
import hlhj.fhp.checkpoint.atys.CostAty
import hlhj.fhp.checkpoint.atys.PayAty
import hlhj.fhp.checkpoint.iview.ActiveView
import hlhj.fhp.checkpoint.javaBean.AliPayBean
import hlhj.fhp.checkpoint.javaBean.CarPageBean
import hlhj.fhp.checkpoint.javaBean.DeleteCarEvent
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.fgm_point_info.view.*
import org.greenrobot.eventbus.EventBus

/**
 * Created by Never Fear   on 2018\11\7 0007.
Never More....
 */
class CarPageFgm :BaseFgm<BaseView,BasePresenter<*,BaseView>>(), BaseView, ActiveView {
    private var type=0 //0信息 1添加
    private var costId:Int?=-1;
    private var num=""
    private var info:CarPageBean.InfoBean?=null
    private val mHandler= @SuppressLint("HandlerLeak")
    object : Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            val result = msg!!.obj as Map<String, String>
            log("fhp","回调为${result.toString()}")
            val status = result["resultStatus"]
            ToastUtils.toast("支付成功",context!!)
            if (status.equals("9000")){
                Constant.isActive=true
            }else{

            }
        }
    }
    companion object {
        fun getInstance(type:Int,infoBeanStr:String): CarPageFgm {
            val carPageFgm = CarPageFgm()
            val bundle = Bundle()
            bundle.putInt("type",type)
            bundle.putString("info",infoBeanStr)
            carPageFgm.arguments=bundle
            return carPageFgm
        }
    }
    override fun initView() {

        if (type==0){
            rootView.loInfo.visibility=View.VISIBLE
            rootView.btPhoto.visibility=View.GONE

            rootView.plateNum.text=info?.plate_number
            if (info?.untreated==0){
                rootView.tv1.text="无未处理违章"
            }else{
                rootView.tv1.text="${info?.untreated}未处理违章"
            }
            rootView.tvMoney.text="罚款${info?.total_fine}元"
            rootView.tvCost.text="扣分${info?.total_points}分"
            rootView.carPoint.text="${12-info?.total_points!!}"

        }else{

            rootView.loInfo.visibility=View.GONE
            rootView.btPhoto.visibility=View.VISIBLE
        }
    }
    override fun logicStart() {
    }

    override fun setListener() {
        rootView.btPhoto.setOnClickListener {

            if (!Constant.isActive){
                ToastUtils.toast("前去激活用户",context!!)
                val intent = Intent(context, PayAty::class.java)
                intent.putExtra("id","")
                intent.putExtra("money",Constant.Active_Money)
                intent.putExtra("orderType",2)
                startActivity(intent)
            }else{
                startActivityForResult(Intent(context,AddCarAty::class.java),1)
            }
        }
        rootView.btCheck.setOnClickListener {
            val intent = Intent(context, CostAty::class.java)
            intent.putExtra("id",costId)
            intent.putExtra("num",info?.plate_number)
            context?.startActivity(intent)
        }

        rootView.btDelet.setOnClickListener {
            AlertDialog.Builder(context!!)
                    .setTitle("确定删除这辆车？")
                    .setPositiveButton("确定"){
                        dialog, which ->
                        val carEvent = DeleteCarEvent()
                        carEvent.id=costId
                        EventBus.getDefault().post(carEvent)
                        dialog.dismiss()
                    }.setNegativeButton("取消"){
                dialog, which ->
                dialog.dismiss()
            }
                    .show()
        }

    }

    private val SDK_PAY_FLAG: Int=1201

    override fun setActiveOrderSign(beanStr: String) {
        val payBean = Gson().fromJson(beanStr, AliPayBean::class.java)
        val orderInfo = payBean.info   // 订单信息

        val payRunnable = Runnable {
            val alipay = PayTask(activity!!)
            val result = alipay.payV2(orderInfo, true)

            val msg = Message()
            msg.what = SDK_PAY_FLAG
            msg.obj = result
            mHandler.sendMessage(msg)
        }
        // 必须异步调用
        val payThread = Thread(payRunnable)
        payThread.start()
    }

    override fun bindView(): BaseView {
        return this
    }


    override fun createPresenter(): BasePresenter<*, BaseView> {
        return object :BasePresenter<BaseModule,BaseView>(){
            override fun createModule() {

            }
        }
    }

    override fun doBeforInit() {
        arguments?.let {
            type=it.getInt("type")
            info=Gson().fromJson(it.getString("info"),CarPageBean.InfoBean::class.java)
            costId=info?.id
        }
    }

    override fun getContentId(): Int {
        return R.layout.fgm_point_info
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        log("fhp","---------------FGM里面")
    }
}